<template>
  <a-card :bordered="false">
    <a-spin :spinning="confirmLoading">
      <h2 style="text-align: center;">撤销异地管控-转出省外申请表</h2>
      <j-form-container :disabled="disabled">
        <a-form-model layout="horizontal" ref="ruleForm" slot="detail" :model="model" :rules="validatorRules" v-bind="formItemLayout">
          <z-form-item :datas="fieldArr" :form="model" :rules="validatorRules" :gutter="0">
            <!--//自定义身份证号-->
            <template slot="slots-identityCard">
              <a-space>
                <a-input v-model="model.identityCard" placeholder="请输入身份证号" style="width: 500px;" />
                <a-button @click="onVerify">获取验证</a-button>
              </a-space>
            </template>
          </z-form-item>
          <a-form-model-item :wrapper-col="{ offset: 6 }">
            <a-space :size="20" style="margin-top:30px;">
              <a-button @click="onResetForm">重置</a-button>
              <a-button type="primary" @click="submitForm">提交</a-button>
            </a-space>
          </a-form-model-item>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </a-card>
</template>

<script>
import { outOtherProvinceApply, getOutsideByIdentityCard } from '@/api/sjsk/personService'
  export default {
    data () {
      return {
        model: {},
        confirmLoading: false,
        disabled: false,
        formItemLayout: {
          labelCol: { span: 6 },
          wrapperCol: { span: 12 }
        },
        fieldArr: [
          {label: '身份证号', prop: 'identityCard'},
          {label: '姓名', prop: 'name', tagType: 'input', disabled: true},
          {label: '别名/绰号', prop: 'nickName', tagType: 'input', disabled: true},
          {label: '籍贯', prop: 'country', tagType: 'dict-select', dictCode:'country', disabled: true},
          {label: '民族', prop: 'nation', tagType: 'dict-select', dictCode:'nation', disabled: true},
          {label: '出生日期', prop: 'birthday', tagType: 'date-picker', format: 'YYYY-MM-DD', showTime:false, disabled: true},
          {label: '性别', prop: 'sex', tagType: 'dict-select', dictCode:'sex', disabled: true},
          {label: '手机号码', prop: 'phone', tagType: 'input', disabled: true},
          {label: '涉网信息情况', prop: 'networdInfoSituation', tagType: 'textarea', placeholder: '请输入人员涉网信息（QQ 、微信号、微博、抖音号、快手号、论坛账号等各类涉网信息）', disabled: true},
          {label: '户籍地详址', prop: 'householdAddress', tagType: 'input', disabled: true},
          {label: '户籍地派出所', prop: 'householdPoliceStation', tagType: 'input', disabled: true},
          {label: '居住地详址', prop: 'currentAddress', tagType: 'input', disabled: true},
          {label: '居住地执行社区', prop: 'currentLevels', tagType: 'linkage-area', disabled: true},
          {label: '居住地派出所', prop: 'currentPoliceStation', tagType: 'input', disabled: true},
          {label: '当前管控状态（类型）', prop: 'currentControlStatus', tagType: 'dict-select', dictCode:'current_control_status', disabled: true},
          {label: '风险类别', prop: 'riskCategory', tagType: 'dict-select', dictCode:'risk_category', disabled: true},
          {label: '居住地专干姓名', prop: 'belongManagerName', tagType: 'input', disabled: true},
          {label: '专干联系电话', prop: 'belongManagerPhone', tagType: 'input', disabled: true},
          {label: '居住地民警姓名', prop: 'policeName', tagType: 'input', disabled: true},
          {label: '民警联系电话', prop: 'policePhone', tagType: 'input', disabled: true},
          {label: '申请撤销管控时间', prop: 'applyControlDate', tagType: 'date-picker', format: 'YYYY-MM-DD'},
          {label: '申请撤销异地管控原因', prop: 'applyControlReason', tagType: 'textarea',},
          {label: '申请人姓名', prop: 'applyName', tagType: 'input'},
          {label: '申请人电话', prop: 'applyPhone', tagType: 'input'},
          {label: '证明材料', prop: 'file', tagType: 'upload-file', fileDescribe: '撤销异地管控的理由依据或者居住地住址、就业、就读等相关证明材料附件；', fileFormat: '.doc,.docx,.xls,.xlsx,.pdf,.jpg,.jpeg,.png'},
          {label: '吸毒人员存档材料', prop: 'drugFile', tagType: 'upload-file', fileDescribe: '可上传该社戒社康人员多个材料，进行存档'},
        ],
        validatorRules: {
          identityCard:[ { required: true, message: '请输入身份证号' }],
          applyControlDate:[ { required: true, message: '请选择申请撤销管控时间' }],
          applyControlReason:[ { required: true, message: '请输入申请撤销异地管控原因' }],
          applyName:[ { required: true, message: '请输入申请人姓名' },],
          applyPhone:[ { required: true, message: '请输入申请人电话' },],
          file:[ { required: true, message: '请上传证明材料' },]
        }
      }
    },
    methods: {
      onVerify() {
        this.$refs['ruleForm'].validateField('identityCard',(error)=> {
          if (!error) {
            getOutsideByIdentityCard(this.model.identityCard)
              .then(res => {
                if (res.success) {
                  const { result } = res
                  if (!result) {
                    this.$message.warn('请录入正确的身份证信息！')
                    this.model = {}
                    return
                  }
                  let model = {
                    controlPersonId: result.controlPersonId,
                    controlPersonType: result.controlPersonType,
                    controlStatus: 2, // 管控状态：1、异地管控 2、撤销异地管控
                    name: result.realName,
                    nickName: result.nickName,
                    country: result.country,
                    nation: result.nation,
                    birthday: result.birthday,
                    sex: result.sex,
                    phone: result.phone,
                    networdInfoSituation: result.networdInfoSituation,
                    householdAddress: result.householdAddress,
                    householdPoliceStation: result.householdPoliceStation,
                    currentAddress: result.currentAddress,
                    currentPoliceStation: result.currentPoliceStation,
                    currentLevels: result.currentLevel?result.currentLevel.split(','): [],
                    currentLevel: result.currentLevel,
                    currentControlStatus: result.currentControlStatus,
                    riskCategory: result.riskCategory,
                    belongManagerName: result.belongManagerName,
                    belongManagerPhone: result.belongManagerPhone,
                    policeName: result.policeName,
                    policePhone: result.policePhone,
                  }
                  this.model = Object.assign({}, this.model, model )
                } else {
                  this.$message.warn(res.message?res.message: '操作失败')
                }
              })
              .catch(e => {
                this.$message.warn('请求失败！')
                this.confirmLoading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      submitForm () {
        const parameter = Object.assign({}, this.model)
        this.$refs.ruleForm.validate((valid)=> {
          if (valid) {
            this.confirmLoading = true
            outOtherProvinceApply(parameter)
              .then(res => {
                this.confirmLoading = false
                if (res.success) {
                  this.$message.success(res.message)
                } else {
                  this.$message.warn(res.message)
                }
              })
              .catch(e => {
                this.$message.warn('请求失败！')
                this.confirmLoading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      onResetForm() {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
</script>
